package bch.rocketmq.test;

public class DaemonExitDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread daemon = new Thread(() -> {
            try {
                while(true) {
                    System.out.println("守护线程运行中...");
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                // 强制终止守护线程, 不会执行 finally 块
                System.out.println("守护线程 finally 执行了");
            }
        });
        daemon.setDaemon(true);
        daemon.start();

        Thread.sleep(2000);
        System.out.println("主线程结束，JVM退出");
    }
}
